
goog.provide('esn.NetworkCreator');

goog.require('esn.BaseSurvey');
goog.require('esn.FriendSurveyList');
goog.require('esn.constants');
goog.require('esn.dialog.AlertDialog');
goog.require('esn.model.FriendSurveyModel');
goog.require('esn.model.StudentModel');
goog.require('esn.soy.networkCreator');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.ui.AutoComplete.Basic');
goog.require('goog.ui.CustomButton');
goog.require('goog.ui.LabelInput');


/**
 * @constructor
 */
esn.NetworkCreator = function(appContext, students, opt_domHelper) {
  goog.base(this, appContext, esn.NetworkCreator.SECTION_ID, opt_domHelper);

  this.appContext_ = appContext;

  this.surveyClient_ = this.appContext_.getService(
      esn.constants.ServiceId.SURVEY_CLIENT);

  this.students_ = students;
};
goog.inherits(esn.NetworkCreator, esn.BaseSurvey);

esn.NetworkCreator.SECTION_ID = 'network_creator';

esn.NetworkCreator.EventType = {
  BACK: goog.events.getUniqueId('back'),
  CONTINUE: goog.events.getUniqueId('continue')
};

esn.NetworkCreator.prototype.autocomplete_;

esn.NetworkCreator.prototype.friendSurveryList_;

esn.NetworkCreator.prototype.continueButton_;

esn.NetworkCreator.prototype.backButton_;

esn.NetworkCreator.prototype.numSelectedEl_;

esn.NetworkCreator.prototype.createDom = function() {
  goog.base(this, 'createDom');

  this.setElementInternal(soy.renderAsElement(esn.soy.networkCreator.content,
      {NumSelected: 0}));

  this.autocomplete_ = new goog.ui.AutoComplete.Basic(this.students_,
      this.getElementByClass('friendAutocompleteInput'), false);
  var label = new goog.ui.LabelInput();
  label.decorate(this.getElementByClass('friendAutocompleteInput'));

  this.friendSurveryList_ = new esn.FriendSurveyList(this.appContext_,
      this.getDomHelper());
  this.friendSurveryList_.render(this.getElementByClass('friendListContainer'));

  this.surveyClient_.getFriendSurveys(
      goog.bind(this.getFriendSurveysResponse_, this));

  this.continueButton_ = new goog.ui.CustomButton(
      esn.constants.getContinueButtonCaption());
  this.continueButton_.render(this.getElementByClass('continueButton'));

  this.backButton_ = new goog.ui.CustomButton(
      esn.constants.getBackButtonCaption());
  this.backButton_.render(this.getElementByClass('backButton'));

  this.numSelectedEl_ = this.getElementByClass('numSelected');
};

esn.NetworkCreator.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this.autocomplete_,
      goog.ui.AutoComplete.EventType.UPDATE,
      this.handleAutocompleteSelect_);

  this.getHandler().listen(
      this.continueButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleContinueClick_);

  this.getHandler().listen(
      this.backButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleBackClick_);

  this.getHandler().listen(
      this.friendSurveryList_,
      esn.FriendSurveyList.EventType.FRIEND_REMOVED,
      this.updateNumSelectedElement_);
};

esn.NetworkCreator.prototype.getFriendSurveys = function() {
  var friendSurveyModels = [];
  this.friendSurveryList_.forEachChild(function(friendSurvey) {
    friendSurveyModels.push(friendSurvey.getModel());
  });
  return friendSurveyModels;
};

esn.NetworkCreator.prototype.getFriendSurveysResponse_ = function(response) {
  if (!response.hasError()) {
    var friendSurveys = response.getData();
    this.friendSurveryList_.addFriends(friendSurveys);
    this.updateNumSelectedElement_();
  }
};

esn.NetworkCreator.prototype.handleAutocompleteSelect_ = function(e) {
  if (this.friendSurveryList_.getChildCount() < 15) {
    this.friendSurveryList_.addFriend(e.row, true);
    this.updateNumSelectedElement_();
  } else {
    this.displayNotEnoughSelectedDialog_();
  }
  this.getElementByClass('friendAutocompleteInput').value = '';
};

esn.NetworkCreator.prototype.handleContinueClick_ = function(e) {
  if (this.friendSurveryList_.getChildCount() >= 4) {
    this.dispatchEvent(esn.NetworkCreator.EventType.CONTINUE);
    // Dispatch change event to update progress bar.
    this.dispatchEvent(new esn.BaseSurvey.ChangeEvent(
        esn.NetworkCreator.SECTION_ID, 1, 1));
  } else {
    this.displayTooManySelectedDialog_();
  }
};

esn.NetworkCreator.prototype.handleBackClick_ = function(e) {
  this.dispatchEvent(esn.NetworkCreator.EventType.BACK);
};

esn.NetworkCreator.prototype.updateNumSelectedElement_ = function() {
  this.getDomHelper().setTextContent(this.numSelectedEl_,
      this.friendSurveryList_.getChildCount());
};

esn.NetworkCreator.prototype.displayNotEnoughSelectedDialog_ = function() {
  var title = 'Too many residents added!';
  var message = 'You can only add up to 15 residents. If you wish to add ' +
      'this resident, please remove one of the residents below.';
  var alertDialog = new esn.dialog.AlertDialog(title, message);
  alertDialog.setDisposeOnHide();
  alertDialog.setVisible(true);
};

esn.NetworkCreator.prototype.displayTooManySelectedDialog_ = function() {
  var title = 'Too few residents added!';
  var message = 'You must add at least 4 residents to continue.';
  var alertDialog = new esn.dialog.AlertDialog(title, message);
  alertDialog.setDisposeOnHide();
  alertDialog.setVisible(true);
};
